import 'package:flutter/material.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/video/cartoon/video_detail_cartoon_view.dart';
import 'package:tiktokadult/hj_page/video/view/video_detail_view.dart';
import 'package:video_player/video_player.dart';

import '../../../hj_model/acg/comic_chapters_model.dart';
import '../../../hj_utils/date_time_util.dart';
import '../../comment/comment_views.dart';

class VideoTabBarView extends StatefulWidget {
  final VideoModel? model;
  final ComicChapterInfo? currentEpisode;
  final VideoPlayerController? controller;
  final PageController pageController;
  final Function? callback;
  final bool? isCartoon;
  final Function(VideoModel model)? vmCallback; // 更多选择回掉
  final Function(ComicChapterInfo model)? episoCallback; //动漫选集回掉
  final Function(int style)? unCodeCallback; // 1:源视频,2:中文字幕,3:破解版

  const VideoTabBarView({
    super.key,
    this.model,
    this.controller,
    this.callback,
    this.vmCallback,
    this.currentEpisode,
    this.episoCallback,
    this.unCodeCallback,
    required this.pageController,
    this.isCartoon,
  });

  @override
  State<StatefulWidget> createState() {
    return _VideoTabBarViewState();
  }
}

class _VideoTabBarViewState extends State<VideoTabBarView> with AutomaticKeepAliveClientMixin {
  VideoModel? get videoModel => widget.model;
  late String dateTime = DateTimeUtil.format2utc(DateTime.now()) ?? "";

  VideoPlayerController? get videoPlayerController => widget.controller;

  PageController get pageController => widget.pageController;

  bool get isCartoon {
    return widget.isCartoon ?? videoModel?.videoType == 1;
  }

  int get commentPageIndex {
    return 1;
  }

  int selectIndex = 0;
  bool isLoading = false;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    if (widget.pageController.hasClients) {
      widget.pageController.jumpTo(0);
    }
  }

  @override
  void didUpdateWidget(covariant VideoTabBarView oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (isCartoon) {
      return PageView(
        controller: pageController,
        children: [
          VideoDetailCartoonView(
            model: videoModel,
            currentEpisode: widget.currentEpisode,
            controller: videoPlayerController,
            callback: widget.callback,
            vmCallback: widget.vmCallback,
            episoCallback: widget.episoCallback,
          ),
          Container(
            padding: EdgeInsets.only(top: 12),
            child: CommentView(
              videoModel?.id ?? '',
              objType: "cartoon",
            ),
          ),
        ],
      );
    } else {
      return PageView(
        controller: pageController,
        children: [
          VideoDetailView(
            model: videoModel,
            controller: videoPlayerController,
            callback: widget.callback,
            vmCallback: widget.vmCallback,
            unCodeCallback: widget.unCodeCallback,
          ),
          Container(
            padding: EdgeInsets.only(top: 12),
            child: CommentView(videoModel?.id ?? ''),
          ),
        ],
      );
    }
  }

  @override
  void dispose() {
    super.dispose();
  }
}
